#!/bin/bash
# File Name: show_mha_cluster_list.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2019-03-22 15:56:13
# Function: 显示mha集群的节点信息
#########################################################################
master_ip="192.168.1.159"
function get_slave_host()
{
    host_list=($(mysql -umoshan -p'Actionsky0821!@' -h${1} -P3308 -NBe "show slave hosts"|awk '{print $1}'|sed 's/3308//g'))
    for host in ${host_list[@]}
    do
        if [ "${#host}x" == "6x" ]
        then
            echo "192.168.$(cut -c 1-3 <<< "${host}").$(cut -c 4- <<< "${host}")"|sed 's/\.0/\./g'
        elif [ "${#host}x" == "5x" ]
        then
            echo "192.168.$(cut -c 1-2 <<< "${host}").$(cut -c 3- <<< "${host}")"|sed 's/\.0/\./g'
        elif [ "${#host}x" == "4x" ]
        then
            echo "192.168.$(cut -c 1-1 <<< "${host}").$(cut -c 2- <<< "${host}")"|sed 's/\.0/\./g'
        fi
    done
}
for slave_host in $(get_slave_host ${master_ip})
do
    echo -e "\033[32m${master_ip}"
    echo -e "\033[31m|"
    echo -ne "+----\033[0m"
    echo -e "\033[33m${slave_host}"
    for host in $(get_slave_host ${slave_host})
    do
        echo -ne "\033[31m"
        echo -e "     |"
        echo -e "     +----\033[35m${host}"
    done
    echo -e "\033[0m"
done
